Skip to content

<array>: std::array<T,0> calls constructors and destructors of T #5583

@eisenwave

Description

@eisenwave

Describe the bug

STL/stl/inc/array

Lines 761 to 763 in 7841cf8

conditional_t<disjunction_v<is_default_constructible<_Ty>, _Is_implicitly_default_constructible<_Ty>>, _Ty,
_Empty_array_element>
_Elems[1]{};

A std::array<T,0> is meant to be an array of zero elements, but it's actually an array of one element as long as T is default-constructible in this implementation. This is clearly non-compliant.

Command-line test case

C:\Temp>type repro.cpp
#include <array>
#include <iostream>

struct S { S() { std::cout << "test failure\n"; } };

int main() {
    std::array<S,0> s;
}

C:\Temp>cl /EHsc /W4 /WX /std:c++latest .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.36.32522 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.

repro.cpp
Microsoft (R) Incremental Linker Version 14.36.32522.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

C:\Temp>.\repro.exe
test failure

Expected behavior

The program should print nothing.

STL version

7841cf8

Additional context

This is relevant to P3737R0: std::array is a wrapper for an array!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingvNextBreaks binary compatibility

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions